/* your js go here */
$(function () {

    $("#avatar").click(function () {
        let path = $("#contextPath").val();
        let userId = $("#userId").val();
        let url="";
        if (path===""){
            url = "/user/look?userId="+userId;
        }else {
            url = path+ "/user/look?userId="+userId;
        }

        window.location.href = url;
    });

    // 用户列表
    $(".userPageNum").on("click",function () {
        let page = $(this).text();
        if (''===$("#userSearchName").val()||null===$("#userSearchName").val()){
            window.location.href=$("#contextPath").val()+"/user/getUsers?page="+page;
        }else {
            window.location.href=$("#contextPath").val()+"/user/getUsers?page="+page+"&userSearchName="+$("#userSearchName").val();
        }
    });
    $("#userPre").on("click",function () {
        let startPage = $("#userStartPage").val();
        let pageCount = $("#userPageCount").val();
        let page = parseInt(startPage)-parseInt(pageCount);
        if (page<1){
            layer.msg("没有更多了");
            return;
        }
        if (''===$("#userSearchName").val()||null===$("#userSearchName").val()){
            window.location.href=$("#contextPath").val()+"/user/getUsers?page="+page;
        }else {
            window.location.href=$("#contextPath").val()+"/user/getUsers?page="+page+"&userSearchName="+$("#userSearchName").val();
        }
    });
    $("#userNext").on("click",function () {
        let startPage = $("#userStartPage").val();
        let pageCount = $("#userPageCount").val();
        let totalPage = $("#userTotalPage").val();
        let page = parseInt(startPage)+parseInt(pageCount);
        if (page>totalPage){
            layer.msg("没有更多了");
            return;
        }
        if (''===$("#userSearchName").val()||null===$("#userSearchName").val()){
            window.location.href=$("#contextPath").val()+"/user/getUsers?page="+page;
        }else {
            window.location.href=$("#contextPath").val()+"/user/getUsers?page="+page+"&userSearchName="+$("#userSearchName").val();
        }
    });
    $(".attention").on("click",function () {
        let focus = $(this).prop("checked");
        let ufid = $(this).val();
        // let $(this).prop("ufid");
        console.log(focus);
        console.log(ufid);
        $.post($("#contextPath").val()+"/user/attentionUser",{ufid:ufid,focus:focus},function (data) {
            layer.msg(data)
        })
    });

    //关注列表
    $(".unAttention").on("click",function () {
        let focusId = $(this).attr("focusId");
        console.log(focusId);
        $.post($("#contextPath").val()+"/user/unAttentionUser",{focusId:focusId},function (data) {
            location.reload();
            layer.msg(data)
        })
    });
    $(".userFocusPageNum").on("click",function () {
        let page = $(this).text();
        window.location.href=$("#contextPath").val()+"/user/getFocusUsers?page="+page;
    });
    $("#userFocusPre").on("click",function () {
        let startPage = $("#focusStartPage").val();
        let pageCount = $("#focusPageCount").val();
        let page = parseInt(startPage)-parseInt(pageCount);
        if (page<1){
            layer.msg("没有更多了");
            return;
        }
        window.location.href=$("#contextPath").val()+"/user/getFocusUsers?page="+page;
    });
    $("#userFocusNext").on("click",function () {
        let startPage = $("#focusStartPage").val();
        let pageCount = $("#focusPageCount").val();
        let totalPage = $("#focusTotalPage").val();
        let page = parseInt(startPage)+parseInt(pageCount);
        if (page>totalPage){
            layer.msg("没有更多了");
            return;
        }
        window.location.href=$("#contextPath").val()+"/user/getFocusUsers?page="+page;
    });

    // 文章列表
    $(".articlePageNum").on("click",function () {
        let page = $(this).text();
        if (''===$("#articleSearchName").val()||null===$("#articleSearchName").val()){
            window.location.href=$("#contextPath").val()+"/user/getArticles?page="+page;
        }else {
            window.location.href=$("#contextPath").val()+"/user/getArticles?page="+page+"&articleSearchName="+$("#articleSearchName").val();
        }
    });
    $("#articlePre").on("click",function () {
        let startPage = $("#articleStartPage").val();
        let pageCount = $("#articlePageCount").val();
        let page = parseInt(startPage)-parseInt(pageCount);
        if (page<1){
            layer.msg("没有更多了");
            return;
        }
        if (''===$("#articleSearchName").val()||null===$("#articleSearchName").val()){
            window.location.href=$("#contextPath").val()+"/user/getArticles?page="+page;
        }else {
            window.location.href=$("#contextPath").val()+"/user/getArticles?page="+page+"&articleSearchName="+$("#articleSearchName").val();
        }
    });
    $("#articleNext").on("click",function () {
        let startPage = $("#articleStartPage").val();
        let pageCount = $("#articlePageCount").val();
        let totalPage = $("#articleTotalPage").val();
        let page = parseInt(startPage)+parseInt(pageCount);
        if (page>totalPage){
            layer.msg("没有更多了");
            return;
        }
        if (''===$("#articleSearchName").val()||null===$("#articleSearchName").val()){
            window.location.href=$("#contextPath").val()+"/user/getArticles?page="+page;
        }else {
            window.location.href=$("#contextPath").val()+"/user/getArticles?page="+page+"&articleSearchName="+$("#articleSearchName").val();
        }
    });

    // 收藏列表
    $(".collectPageNum").on("click",function () {
        let page = $(this).text();
        if (''===$("#collectSearchName").val()||null===$("#collectSearchName").val()){
            window.location.href=$("#contextPath").val()+"/user/articleCollect?page="+page;
        }else {
            window.location.href=$("#contextPath").val()+"/user/articleCollect?page="+page+"&collectSearchName="+$("#collectSearchName").val();
        }
    });
    $("#collectPre").on("click",function () {
        let startPage = $("#collectStartPage").val();
        let pageCount = $("#collectPageCount").val();
        let page = parseInt(startPage)-parseInt(pageCount);
        if (page<1){
            layer.msg("没有更多了");
            return;
        }
        if (''===$("#collectSearchName").val()||null===$("#collectSearchName").val()){
            window.location.href=$("#contextPath").val()+"/user/articleCollect?page="+page;
        }else {
            window.location.href=$("#contextPath").val()+"/user/articleCollect?page="+page+"&collectSearchName="+$("#collectSearchName").val();
        }
    });
    $("#collectNext").on("click",function () {
        let startPage = $("#collectStartPage").val();
        let pageCount = $("#collectPageCount").val();
        let totalPage = $("#collectTotalPage").val();
        let page = parseInt(startPage)+parseInt(pageCount);
        if (page>totalPage){
            layer.msg("没有更多了");
            return;
        }
        if (''===$("#collectSearchName").val()||null===$("#collectSearchName").val()){
            window.location.href=$("#contextPath").val()+"/user/articleCollect?page="+page;
        }else {
            window.location.href=$("#contextPath").val()+"/user/articleCollect?page="+page+"&collectSearchName="+$("#collectSearchName").val();
        }
    });

    // 取消收藏/收藏文章
    $("#collectArticle").on("click",function () {
        let isCollect = $(this).attr("collect");
        let articleId = $(this).attr("articleId");
        let url="";
        if ("true"=== isCollect){
            // window.location.href=$("#contextPath")+"/user/collectArticle?articleId="+articleId;
            url="/user/collectArticle"
        } else if ("false" === isCollect) {
            // window.location.href=$("#contextPath")+"/user/unCollectArticle?articleId="+articleId;
            url="/user/unCollectArticle"
        }else {
            // window.location.href=$("#contextPath")+"/user/lookArticle?articleId="+articleId;
            url="/user/lookArticle"
        }
        $.ajax({
            url:url,
            type:"POST",
            data:{articleId:articleId},
            success:function (data) {
                console.log(data);
                if ("0"===data){
                    alert("收藏失败");
                } else if ("1"===data){
                    location.reload();
                    alert("收藏成功");
                }else if ("r"===data){
                    alert("您已收藏过该文章");
                } else if ("s"===data) {
                    location.reload();
                    alert("取消收藏成功");
                }else if ("f"===data){
                    alert("取消失败，请稍候再试");
                } else {
                    alert("服务器繁忙，请稍候再试")
                }
            }
        })
    });

    //file表单选中文件时,让file表单的val展示到showname这个展示框
    $('#myFile').change(function () {
        let file = $(this)[0].files[0];
        let formData = new FormData();
        formData.append("photo",file);
        $.ajax({
            url:$("#contextPath").val()+"/user/uploadFile",
            type:"POST",
            data:formData,
            processData:false,
            contentType:false,
            success:function (data) {
                console.log(data);
                $("#headPhoto").attr("src",data);
                $("#userInfoPhoto").attr("src",data);
            }
        })

    });

    //用户列表，查看详情，通过class名字userDetail来找
    $(".userDetail").click(function () {
        console.log("你点击了用户详情");
        window.location.href = '/user/getUserInfo?userId='+$(this).attr("userId");
    });

    //发布会议跳转链接
    $("#addMeet").click(function () {
        window.location.href = '../html/meeting_add.jsp'
    });

    //添加文章跳转链接
    $("#addArt").click(function () {
        window.location.href = '/assets/html/article_add.jsp'
    });


    $(".myList>ul>li").on("click",function () {
        if($(this).find("div").attr('class')=='list-group myGroup isHidden'){
            $(this).find("div").removeClass("isHidden")
        }else{
            $(this).find("div").addClass("isHidden")
        }


    });

    /*$(".table").find("input[type='checkbox']").on("click",function () {
        if($(this).prop("checked")){
            layer.msg("关注成功")
        }else{
            layer.msg("取关成功")

        }
    });*/

    $('.chosen').chosen({
        no_results_text: "没有找到结果！",//搜索无结果时显示的提示
        search_contains:true,   //关键字模糊搜索。设置为true，只要选项包含搜索词就会显示；设置为false，则要求从选项开头开始匹配
        allow_single_deselect:true, //单选下拉框是否允许取消选择。如果允许，选中选项会有一个x号可以删除选项
        disable_search: false, //禁用搜索。设置为true，则无法搜索选项。
        disable_search_threshold: 0, //当选项少等于于指定个数时禁用搜索。
        inherit_select_classes: true, //是否继承原下拉框的样式类，此处设为继承
        placeholder_text_single: '选择国家', //单选选择框的默认提示信息，当选项为空时会显示。如果原下拉框设置了data-placeholder，会覆盖这里的值。
        width: '400px', //设置chosen下拉框的宽度。即使原下拉框本身设置了宽度，也会被width覆盖。
        max_shown_results: 1000, //下拉框最大显示选项数量
        display_disabled_options: false,
        single_backstroke_delete: false, //false表示按两次删除键才能删除选项，true表示按一次删除键即可删除
        case_sensitive_search: false, //搜索大小写敏感。此处设为不敏感
        group_search: false, //选项组是否可搜。此处搜索不可搜
        include_group_label_in_selected: true //选中选项是否显示选项分组。false不显示，true显示。默认false。
    });


});




//触发隐藏的file表单
function makeThisfile() {
    $('#myFile').click();

}